package com.example.project.projectspringcloud.service.impl.user;

import com.example.project.projectspringcloud.common.Constants;
import com.example.project.projectspringcloud.dao.sys.SysConfigMapper;
import com.example.project.projectspringcloud.dao.user.UserProfileMapper;
import com.example.project.projectspringcloud.entity.Page;
import com.example.project.projectspringcloud.entity.user.UserProfile;
import com.example.project.projectspringcloud.service.BaseService;
import com.example.project.projectspringcloud.service.user.UserProfileService;
import com.example.project.projectspringcloud.service.user.UserService;
import com.example.project.projectspringcloud.utils.DateUtil;
import com.example.project.projectspringcloud.utils.HttpUtil;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserProfileServiceImpl extends BaseService<UserProfile, UserProfileMapper> implements UserProfileService {


    private static final Logger logger = LoggerFactory.getLogger(UserProfileServiceImpl.class);

    @Autowired
    private UserService userService;

    @Autowired
    private SysConfigMapper sysConfigMapper;

    /**
     * 下载图片
     *
     * @param urlString
     * @return
     * @throws Exception
     */
    public String download(String urlString) throws Exception {
        // 构造URL
        URL url = new URL(urlString);
        // 打开连接
        URLConnection con = url.openConnection();
        // 设置请求超时为5s
        con.setConnectTimeout(5 * 1000);
        // 输入流
        InputStream is = con.getInputStream();

        // 1K的数据缓冲
        byte[] bs = new byte[1024];
        // 读取到的数据长度
        int len;

        String filename = System.currentTimeMillis() + "";
        // 图片后缀
        String ext = "jpg";

        // 保存路径
        String savePath = "/img/upload/customer/" + DateUtil.toString(new Date(), "yyyyMMdd") + "/";

        // 输出的文件流
        File sf = new File(Constants.contextPath + savePath);
        if (!sf.exists()) {
            sf.mkdirs();
        }
        OutputStream os = new FileOutputStream(sf.getPath() + "/" + filename + "." + ext);
        // 开始读取
        while ((len = is.read(bs)) != -1) {
            os.write(bs, 0, len);
        }
        // 完毕，关闭所有链接
        os.close();
        is.close();

        return savePath + filename + "." + ext;
    }

    @Override
    public UserProfile get(UserProfile entity) {
        return super.mapper.selectOne(entity);
    }

    @Override
    public List<UserProfile> list(Page page, UserProfile entity) {
        return super.list(page, entity);
    }

    @Override
    public boolean save(UserProfile entity) {
        return super.save(entity);
    }

    @Override
    public boolean delete(Integer[] ids) {
        return super.delete(ids);
    }

    /**
     * 下载图片
     *
     * @param urlString
     * @param param
     * @return
     * @throws Exception
     */
    public String download(String urlString, String param) throws Exception {
        Map<String, String> map = new HashMap<>();
        map.put("imageUrl", urlString);
        map.put("param", param);
        String result = HttpUtil.post(Constants.uploadServer + "/image/download", map);
        Map<String, String> resultMap = new Gson().fromJson(result, Map.class);
        if (Boolean.valueOf(resultMap.get("success"))) {
            return resultMap.get("entity");
        } else {
            return "";
        }
    }

}
